#include "VCPPDiffusion2DADI.h"

using namespace VCPPDiffusion2D;
using namespace System;
using namespace System::IO;

void PrepareInitialValues(array<double> ^, VCPPDiffusion2DADI ^);
void Print(VCPPDiffusion2DADI ^, array<double> ^, String ^ outfile);

int main(array<System::String ^> ^args) {

	int endCount = 100;
	VCPPDiffusion2DADI ^ sim = gcnew VCPPDiffusion2DADI();
	sim->BoundaryCondition = 0.0;
	sim->NumberOfNodesInX = 101;
	sim->NumberOfNodesInY = 101;
	sim->DeltaX = 1.0 / 100;
	sim->DeltaY = 1.0 / 100;
	sim->DeltaT = 0.01;
	sim->Alpha = 0.005;

	array<double>^ u = gcnew array<double>(101 * 101);
	PrepareInitialValues(u, sim);

	for (int i = 0; i <= endCount; i++) {
		double timeInMiliseconds = i * sim->DeltaT * 1000.0;
		Print(sim, u, "c:/nsworkspace/vcppadi2d_" 
			+ timeInMiliseconds.ToString("00000") + ".txt");

		sim->NextStep(u);
	}
	return 0;
}

void PrepareInitialValues(array<double> ^ u, VCPPDiffusion2DADI ^ sim) {
	for (int j = 0; j < sim->NumberOfNodesInY; j++) {
		for (int i = 0; i < sim->NumberOfNodesInX; i++) {
			double x = i * sim->DeltaX;
			double y = j * sim->DeltaY;

			u[j * sim->NumberOfNodesInX + i] = 
				Math::Exp(-0.5 * Math::Pow((x - 0.5) / 0.1, 2.0) 
				          -0.5 * Math::Pow((y - 0.5) / 0.1, 2.0));

			if (i == 0 || i == sim->NumberOfNodesInX - 1 ||
				j == 0 || j == sim->NumberOfNodesInY - 1) {
					u[j * sim->NumberOfNodesInX + i] = 0.0;
			}
		}
	}

}

void Print(VCPPDiffusion2DADI ^ sim, array<double> ^ u, String ^ outfile) {

	StreamWriter ^ writer = File::CreateText(outfile);
	for (int i = 0; i < sim->NumberOfNodesInX; i++) {
		for (int j = 0; j < sim->NumberOfNodesInY; j++) {
			double x = i * sim->DeltaX;
			double y = j * sim->DeltaY;
			writer->WriteLine(x + " " + y + " " + u[j * sim->NumberOfNodesInX + i]);
		}
		writer->WriteLine();
	}
	writer->Close();
}



